﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using MySql.Data.MySqlClient;

class NormalMsgDataBase
{
    private static NormalMsgDataBase instance = null;
    public static NormalMsgDataBase Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new NormalMsgDataBase();
            }
            return instance;
        }
    }
    private MySqlConnection conn;
    public void Init()
    {
        conn = new MySqlConnection("server=localhost;User Id=root;password=;port=3306;" +
            "Database=cleaningdiary;Charset=utf8mb4;");
        conn.Open();
    }
    public PlayerData QueryNormalDataOfPlayerData(string username, uint password)
    {
        bool isnew = true;
        PlayerData playerData = null;
        MySqlDataReader reader = null;
        try
        {
            MySqlCommand cmd = new MySqlCommand("select * from usernormalmsg where" +
                " username=@username", conn);//*表示选择所有符合条件，where表示选这符合后面条件的

            cmd.Parameters.AddWithValue("username", username);
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                isnew = false;
                uint _pass = reader.GetUInt32("password");
                playerData = new PlayerData
                {
                    mode = reader.GetString("mode"),
                    roomid = reader.GetUInt32("roomid")
                };
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Query NormalData By Acct&Pass Error:" + e);
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (isnew)
            {
                //不存在账号数据，创建新的默认账号数据，并返回
                playerData = new PlayerData
                {
                    mode="",
                    roomid = 0
                };
            }
        }
        return playerData;
    }
    public bool QueryNormalDataOfUsernameExist(string username, uint password,out MsgLoginRsp msgLoginRsp,out bool canFind)
    {
        MySqlDataReader reader = null;
        msgLoginRsp = null;
        try
        {
            MySqlCommand cmd = new MySqlCommand("select * from usernormalmsg where" +
                " username=@username", conn);//*表示选择所有符合条件，where表示选这符合后面条件的

            cmd.Parameters.AddWithValue("username", username);
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                canFind = true;
                uint _pass = reader.GetUInt32("password");
                if (_pass.Equals(password))
                {
                    msgLoginRsp = new MsgLoginRsp
                    {
                        errorCode = MsgLoginRsp.ErrorCode.success
                    };
                    reader.Close();
                    return true;
                }
                else 
                {
                    msgLoginRsp = new MsgLoginRsp
                    {
                        errorCode = MsgLoginRsp.ErrorCode.passwordError
                    };
                    reader.Close();
                    return false;
                }
            }
            else
            {
                canFind= false;
                msgLoginRsp = new MsgLoginRsp 
                {
                    errorCode=MsgLoginRsp.ErrorCode.usernameError
                };
                reader.Close();
                return false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Query NormalData By Acct&Pass Error:" + e);
            msgLoginRsp=new MsgLoginRsp
            { 
                errorCode=MsgLoginRsp.ErrorCode.fail
            };
            canFind = false;
        }
        return false;
    }

    public bool InsertNewNormalData(string username, uint password,out bool canFind)
    {
        MsgLoginRsp msgLoginRsp;
        QueryNormalDataOfUsernameExist(username, password,out msgLoginRsp,out canFind);
        if (!canFind)
        {
            try
            {
                MySqlCommand cmd = new MySqlCommand("insert into usernormalmsg set username" +
                    "=@username,password=@password", conn);
                cmd.Parameters.AddWithValue("username", username);
                cmd.Parameters.AddWithValue("password", password);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Insert NormalData Error:" + e);
                return false;
            }
        }
       
        return false;
    }
    public bool UpdateNormalData(PlayerData playerData)
    {
        try
        {
            MySqlCommand cmd = new MySqlCommand("update usernormalmsg set password=@password,mode=" +
                "@mode,roomid=@roomid where username=@username", conn);
            cmd.Parameters.AddWithValue("username", playerData.username);
            cmd.Parameters.AddWithValue("password", playerData.password);
            cmd.Parameters.AddWithValue("mode", playerData.mode);
            cmd.Parameters.AddWithValue("roomid", playerData.roomid);
            cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine("Update NormalData Error:" + e);
            return false;
        }
        return true;
    }
    public bool DeleteNormalData(string username)
    {
        try
        {
            MySqlCommand cmd = new MySqlCommand("delete from usernormalmsg where username=@username", conn);
            cmd.Parameters.AddWithValue("username", username);
            cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine("Update NormalData Error:" + e);
            return false;
        }
        return true;
    }
}
